﻿<?php
	set_time_limit(0);
	require_once("config.inc.php");
	require_once("charset.func.php");
	require_once("db.class.php");
	$db = new db($site_Db);
	/*
	function make_File( $fileName, $content = "" )
	{
		$path = substr( $fileName, 0, strrpos( $fileName, "/" ) );
		make_Dir( $path );
		$fp = fopen( $fileName, "w+" );
		fwrite( $fp, $content );
		fclose( $fp );
		return true;
	}
	
	function make_Dir( $path )
	{
		$path = str_replace( "\\", "/", $path );
		if ( file_exists( $path ) )
		{
			return true;
		}
		$dirs = explode( "/", $path );
		$dir_tmp = "";
		$i = 0;
		for ( ;	$i < count( $dirs );	++$i	)
		{
			$dir_tmp .= $dirs[$i]."/";
			if ( !file_exists( $dir_tmp ) )
			{
				mkdir( $dir_tmp );
			}
		}
		return true;
	}
	
	function read_File( $file )
	{
		$fp = fopen( $file, "r" );
		flock( $fp, 1 );
		$content = fread( $fp, filesize( $file ) );
		fclose( $fp );
		return $content;
	}
	
	function write_File( $file, $content, $mode = "w+" )
	{
		if ( !file_exists( $file ) )
		{
			make_File( $file );
		}
		$fp = fopen( $file, $mode );
		flock( $fp, 2 );
		$result = fwrite( $fp, $content );
		fclose( $fp );
		return $result;
	}
	*/

	//header( "Content-Type:text/html; charset=gb2312" );
	//$url = 'http://www.nongli.com/item3/searchNL.asp';
	/*
	function get_Calendar($y,$m,$d) {
		global $db;
		$date = $y."-".$m."-".$d;
		$cinfo = $db->fetchone("SELECT cinfo FROM calendar WHERE cdate LIKE '{$date}'");
		//echo $cinfo;
		if(empty($cinfo))
		{
			$url = 'http://www.nongli.com/item3/index.asp';
			$fields = array(
				'year'=>urlencode(strval($y)),
				'mm'=>urlencode(strval($m))
			);
			//url-ify the data for the POST
			foreach($fields as $key=>$value) {
				$fields_string .= $key.'='.$value.'&';
			}
			rtrim($fields_string ,'&');
			//echo $fields_string;
			//ob_start
			ob_start();
			//open connection
			$ch = curl_init();
			//set the url, number of POST vars, POST data 
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_POST,count($fields));
			curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
			//execute post
			curl_exec($ch);
			
			//close connection
			curl_close($ch);
			
			$result=ob_get_contents();
			ob_end_clean();
		//提取万年历信息
			//$result = read_File('./r.txt');
			//write_File('./r.txt',$result);
			$result = gb2utf8($result);
			preg_match_all("/<A\sHREF=\"#\"\sonclick=return\(false\)\stitle=\".+?公历:(.+?)\s(.+?)\">.+?<\/A>/s",$result,$list,PREG_SET_ORDER);
			//print_r($list);
			
			$re="";
			foreach($list as $i) {
				if(!empty($i[1]) && !empty($i[2])) {
					$info=preg_replace("/(&nbsp;)+/", " ", $i[2]);
					if($db->query("REPLACE INTO calendar VALUES('".trim($i[1])."', '".trim($info)."')"))
						echo "Add a new date.";
					if(trim($i[1])==$date)
						$re=$info;
				}
			}
			return $info;
		} else {
			return $cinfo;
		}
	}
	*/
	
	function get_Calendar($y,$m,$d) {
		global $db;
		$date = $y."-".$m."-".$d;
		$cen = substr(strval($y), 0, 2);
		$cinfo = $db->fetchone("SELECT cinfo FROM cal".$cen." WHERE cdate LIKE '{$date}'");
		//echo $cinfo;
		return $cinfo;
	}
	
	//echo get_Calendar(2010,1,1);
	
?>